/**
 * 
 */
package cn.gov.msa.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.gov.msa.dao.RoleMapper;
import cn.gov.msa.dao.UserDetailMapper;
import cn.gov.msa.model.Role;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.model.UserRole;
import cn.gov.msa.pagehelper.CustomPageHelper;
import cn.gov.msa.service.UserDetailService;

/**   
* @Title: UserDetailServiceImpl.java 
* @Package cn.gov.msa.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author 何宇江  
* @date 2018年2月20日 下午2:36:18 
*/
public class UserDetailServiceImpl implements UserDetailService {

	
	@Autowired
	UserDetailMapper userDetailMapper;
	
	@Override
	public List<UserDetail> selectData(UserDetail userDetail) throws Exception {
		List<UserDetail> userDetailList = new ArrayList<UserDetail>();
		
		userDetailList = userDetailMapper.selectData(userDetail);
		
		return userDetailList;
	}

	@Override
	public List<UserDetail> selectData(int pageNum, int pageSize, UserDetail userDetail) throws Exception {

		CustomPageHelper.startPage(pageNum, pageSize); // 核心分页代码  
		List<UserDetail> userDetailList = userDetailMapper.selectData(userDetail);
		
		return userDetailList;
	}

	
	@Override
	@Transactional
	public boolean right(UserDetail userDetail, List<Role> role) throws Exception {
		boolean result = false;
		
		int i = 0;
		
		int iD = userDetailMapper.deleteByUserid(userDetail.getUserid());
		
		for (Role role2 : role) {
			UserRole userRole = new UserRole();
			userRole.setUserid(userDetail.getUserid());
			userRole.setUsername(userDetail.getUsername());
			userRole.setRolecode(role2.getRolecode());
			userRole.setRolename(role2.getRolename());
			userRole.setOrgcode(userDetail.getOrgcode());
			userRole.setOrgname(userDetail.getOrgname());
			
			i = userDetailMapper.right(userRole);
		}
		
		if(i > 0 || role.size() == 0)
			result = true;
		
		
		return result;
	}
	
	@Autowired
	RoleMapper roleMapper;

	@Override
	public List<UserRole> selectUserRole(UserRole userRole) throws Exception {
		List<UserRole> userRoleList = new ArrayList<UserRole>();
		
		userRoleList = userDetailMapper.selectUserRole(userRole);
		
		return userRoleList;
	}
	
}
